package cn.finder.httpcommons.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.util.JSONPObject;

import cn.finder.httpcommons.utils.JsonUtils;

public class JsonReader implements IReader{
	 // Fields
    private Map<String,Object> jsonMap;

    public JsonReader(Map<String,Object> jsonMap)
    {
        this.jsonMap = jsonMap;
    }

    /****
     * @type ListType
     */
    public List getListObjects(String listName, String itemName, Class type,Class itemClassType)
    {
        
    	
    	List list = null;

        if (!"".equals(itemName))
        {
        	Object listValue=this.jsonMap.get(listName);
        	
        	if(listValue instanceof List<?>){
        		
        	}
        	
        	//内嵌的JSON串没有自动转成 Map类型时候，那么手动转换
        	List<Map<String,Object>> dictionary = (List<Map<String, Object>>) this.jsonMap.get(listName);
            if ((dictionary != null) && (dictionary.size() > 0))
            {
               // List list2 = (List)dictionary.get(itemName);
            	List list2=null;
                if ((list2 == null) || (list2.size() == 0))
                {
                    return list;
                }
                list = new ArrayList();
                
            }
        }
        else
        {
        	List list2 = (List)this.jsonMap.get(listName);
            if ((list2 == null) || (list2.size() == 0))
            {
                return list;
            }
            
            
            
            list = new ArrayList();
            
            for(int i=0;i<list2.size();i++){
            	
            	Object current= list2.get(i);
            	
            	if(current instanceof Map){
            		
            		Map json = (Map)current;
            		
            		
            		JsonParser jsonParser = new JsonParser();
            		Object obj3 = jsonParser.fromJson(new JsonReader(json), itemClassType);
            		
            		if(obj3!=null){
            			list.add(obj3);
            		}
            	}  
            	/*else if (!typeof(IList).IsAssignableFrom(current.GetType()))
                {
                    if (typeof(JsonNumber).IsAssignableFrom(current.GetType()))
                    {
                        list.Add(((JsonNumber)current).ToInt64());
                        continue;
                    }
                    list.Add(current);
                }*/
            	
            }
            
            
        }
        return list;
    	

    }


    public Object getPrimitiveObject(Object name)
    {
        return this.jsonMap.get(name);

    }

    public Object getArrayObject(Object name){
    	Object o = this.jsonMap.get(name);
    	//String str=o.toString().replace("[", "").replace("]", "");
    	//String[] strarr=str.split(",");
    	
    	//System.out.println(o.getClass());
    	//System.out.println(o.getClass().getName());
    	
    	List list=(ArrayList)o;
    	Object[] arrData=list.toArray(); //返回数据对象是 Object[]  所以 反射时 需要定义 setXXX(Object[] o) 来获取值
    	return arrData;
    	
    }
    
    public Object getReferenceObject(Object name, Class type/*, DConvert convert*/)
    {
    	Object o = this.jsonMap.get(name);
    	//updatedragon
    	/*if(o instanceof JSONNull){
    		return null;
    		
    	}*/
    	if("null".equals(o)){
    		return null;
    	}
    	
    	
    	if(o instanceof Map){
	    	 Map<String,Object> itemJson =(Map<String,Object>)o;
	        if ((itemJson != null) && (itemJson.size() > 0))
	        {
	        	JsonParser jsonParser = new JsonParser();
	    		Object obj3 = jsonParser.fromJson(new JsonReader(itemJson), type);
	    		
	            return  obj3;
	        }
	        else{
	        	return null;
	        }
    	}
        return o;

    }


    public boolean hasReturnField(Object name)
    {
        return this.jsonMap.containsKey(name);

    }
    
    /***
     * 获取原生的JSONMAP 数据
     * @return
     */
    public Map<String,Object> getData(){
    	return this.jsonMap;
    }

}
